package com.example.admin.smstest.utils;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Calendar;

/**
 * Created by Y on 2017/6/4.
 */

public class Test {
        public static void gettest(){
            String lengths = null;
            String string = "你好啊中国是打发斯蒂芬是的发生发的";
            try {
                byte[] stringByte = string.getBytes("UNICODE");
                System.out.println(Arrays.toString(stringByte));
                int length = stringByte.length;
                if(length < 10){
                    String l161 = Integer.toHexString(length);
                    lengths = "0"+l161;
                }else if(length >= 10 && length <= 127){
                    lengths = Integer.toHexString(length);
                }else if (length > 127){

                }
                byte[] test =hexStringToBytes(lengths);
                System.out.println(bytesToHexString(test));
                System.out.println(Arrays.toString(hexStr2Bytes(lengths)));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            long nowLongTime = System.currentTimeMillis(); //获取时间的long类型。

            String dateString = DateUtil.getTime(nowLongTime);

        }

        public static byte[] hexStringToBytes(String hexString) {
            if (hexString == null || hexString.equals("")) {
                return null;
            }
            hexString = hexString.toUpperCase();
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] d = new byte[length];
            for (int i = 0; i < length; i++) {
                int pos = i * 2;
                d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }
            return d;
        }

        /**
         * Convert char to byte
         * @param c char
         * @return byte
         */
        private static byte charToByte(char c) {
            return (byte) "0123456789ABCDEF".indexOf(c);
        }

        public static String bytesToHexString(byte[] src){
            StringBuilder stringBuilder = new StringBuilder("");
            if (src == null || src.length <= 0) {
                return null;
            }
            for (int i = 0; i < src.length; i++) {
                int v = src[i] & 0xFF;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    stringBuilder.append(0);
                }
                stringBuilder.append(hv);
            }
            return stringBuilder.toString();
        }

        /**
         * bytes字符串转换为Byte值
         *
         * @param src Byte字符串，每个Byte之间没有分隔符
         * @return byte[]
         */
        public static byte[] hexStr2Bytes(String src) {
            int m = 0, n = 0;
            int l = src.length() / 2;
            byte[] ret = new byte[l];
            for (int i = 0; i < l; i++) {
                m = i * 2 + 1;
                n = m + 1;
                ret[i] = Byte.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
            }
            return ret;
        }
    }



